package view.backing.Utils.Validator;

import chemtools_logica.Beans.PersonaBean;

import chemtools_logica.Entidades.Persona;

import chemtools_logica.LN.IR.LN_SFEmpresaRemote;

import chemtools_logica.LN.IR.LN_SFPersonaRemote;

import java.util.Hashtable;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class PersonaValidator implements Validator {
    public PersonaValidator() {
        super();
    }
    LN_SFPersonaRemote lN_SFPersonaRemote;
    @Override
    public void validate(FacesContext facesContext, UIComponent uIComponent, Object object) throws ValidatorException {
        try {
            final Context context = getInitialContext();
            lN_SFPersonaRemote = (LN_SFPersonaRemote) context.lookup("map-LN_SFPersona#chemtools_logica.LN.IR.LN_SFPersonaRemote"); 
        }catch(NamingException e){return;}
        
        PersonaBean p=new PersonaBean();
        p.setDni(object.toString());
        if(lN_SFPersonaRemote.buscaPersona(p).size()>0){
        FacesMessage fm = new FacesMessage("El DNI ya Existe");
        throw new ValidatorException(fm);
        }
    }
    private static Context getInitialContext() throws NamingException {
        Hashtable env = new Hashtable();
        // WebLogic Server 10.x/12.x connection details
        env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
        env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7101");
        return new InitialContext(env);
    }
}
